package com.example.denglu;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.denglu.Appframe.HomeFragment;
import com.example.denglu.bean.Usesfunction;
import com.example.denglu.pages.AiXing.AiXingActivity;
import com.example.denglu.pages.Arr.MainActivity;
import com.example.denglu.pages.ChongWu.ChongWuActivity;
import com.example.denglu.pages.Data.DataActivity;
import com.example.denglu.pages.HuoDon.HuoDonActivity;
import com.example.denglu.pages.WuLiuChaXun.WuLiuChaXunActivity;
import com.example.denglu.pages.YiZhan.YiZhanActivity;
import com.example.denglu.pages.ZhaoGonZuo.ZhaoGonZuoActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class UsesFunActivity extends AppCompatActivity {

    private Button finshfun;
    private RecyclerView usesfunRe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uses_fun);
        initView();
        clickfinsh();
        getimgtext();
    }

    private void getimgtext() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
                RequestBody body = RequestBody.create(mediaType, "");
                Request request = new Request.Builder()
                        .url("http://124.93.196.45:10001/prod-api/api/service/list")
                        .addHeader("Content-Type", "application/x-www-form-urlencoded")
                        .build();
                client.newCall(request).enqueue(new Callback() {

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String string = response.body().string();
                        try {
                            JSONObject jsonObject = new JSONObject(string);
                            int code = jsonObject.getInt("code");
                            if (code == 200) {
                                JSONArray rows = jsonObject.getJSONArray("rows");

                                ArrayList<Usesfunction> usesfunctionArrs = new ArrayList<>();

                                for (int i = 0; i < rows.length(); i++) {
                                    JSONObject rowsjsonObjectarr = (JSONObject) rows.get(i);
                                    Usesfunction usesfunction = new Usesfunction();

                                    usesfunction.setId(rowsjsonObjectarr.getInt("id"));
                                    usesfunction.setName(rowsjsonObjectarr.getString("serviceName"));
                                    usesfunction.setURL("http://124.93.196.45:10001/" + rowsjsonObjectarr.getString("imgUrl"));
//                                    usesfunction.setSort(rowsjsonObjectarr.getInt("id"));

                                    String sort = rowsjsonObjectarr.getString("sort");
                                    if (sort == "null") {
                                        usesfunction.setSort(0);
                                    } else {
                                        usesfunction.setSort(Integer.valueOf(sort).intValue());
                                    }


                                    usesfunctionArrs.add(usesfunction);
                                }

                                for (int i = 0; i < usesfunctionArrs.size(); i++) {
                                    for (int j = i + 1; j < usesfunctionArrs.size(); j++) {
                                        if (usesfunctionArrs.get(i).getSort() < usesfunctionArrs.get(j).getSort()) {
                                            Usesfunction usesfunction0 = usesfunctionArrs.get(i);
                                            usesfunctionArrs.set(i, usesfunctionArrs.get(j));
                                            usesfunctionArrs.set(j, usesfunction0);

                                        }
                                    }
                                }
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
//                                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(App.this);
//                                            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                                        GridLayoutManager gridLayoutManager = new GridLayoutManager(UsesFunActivity.this, 4);
                                        usesfunRe.setLayoutManager(gridLayoutManager);
                                        usesfunRe.setAdapter(new RecyclerView.Adapter() {
                                            private int index = -1;

                                            class myho extends RecyclerView.ViewHolder {
                                                TextView textView;
                                                ImageView imageView;

                                                public myho(@NonNull View itemView) {
                                                    super(itemView);
                                                    textView = itemView.findViewById(R.id.funtitle);
                                                    imageView = itemView.findViewById(R.id.funimg);
                                                }
                                            }

                                            @NonNull
                                            @Override
                                            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                                                View inflate = getLayoutInflater().inflate(R.layout.uses_fun, null);
                                                myho myho = new myho(inflate);
                                                return myho;
                                            }

                                            @Override
                                            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                                                myho holder1 = (myho) holder;

                                                holder1.imageView.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        index = position;
                                                        notifyDataSetChanged();
                                                    }
                                                });

                                                holder1.textView.setText(usesfunctionArrs.get(position).getName());
                                                Glide.with(UsesFunActivity.this)
                                                        .load(usesfunctionArrs.get(position).getURL())
                                                        .into(holder1.imageView);

                                                if (position == index) {
                                                    switch (index) {
                                                        case 0:
                                                            Intent intent = new Intent(UsesFunActivity.this, DataActivity.class);
                                                            startActivity(intent);
                                                            break;
                                                        case 1:
                                                            Intent intent1 = new Intent(UsesFunActivity.this, YiZhanActivity.class);
                                                            startActivity(intent1);
                                                            break;
                                                        case 2:
                                                            Intent intent2 = new Intent(UsesFunActivity.this, AiXingActivity.class);
                                                            startActivity(intent2);
                                                            break;
                                                        case 3:
                                                            Intent intent3 = new Intent(UsesFunActivity.this, WuLiuChaXunActivity.class);
                                                            startActivity(intent3);
                                                            break;
                                                        case 4:
                                                            Intent intent4 = new Intent(UsesFunActivity.this, ChongWuActivity.class);
                                                            startActivity(intent4);
                                                            break;
                                                        case 5:
                                                            Intent intent5 = new Intent(UsesFunActivity.this, HuoDonActivity.class);
                                                            startActivity(intent5);
                                                            break;
                                                        case 6:
                                                            Intent intent6 = new Intent(UsesFunActivity.this, ZhaoGonZuoActivity.class);
                                                            startActivity(intent6);
                                                            break;
                                                        default:
                                                            Intent intent7 = new Intent(UsesFunActivity.this, MainActivity.class);
                                                            startActivity(intent7);
                                                    }
                                                }


                                            }

                                            @Override
                                            public int getItemCount() {
                                                return usesfunctionArrs.size();
                                            }
                                        });
                                    }
                                });

                            } else {

                            }
                        } catch (JSONException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(UsesFunActivity.this, "JOSN转换失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }
                });
                try {
                    Response response = client.newCall(request).execute();
                } catch (IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(UsesFunActivity.this, "请求网络服务器失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }

    private void clickfinsh() {
        finshfun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        finshfun = findViewById(R.id.finshfun);
        usesfunRe = findViewById(R.id.usesfunRe);
    }
}